<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:doc="livcos.org/ns/doc"
  xmlns:c="livcos.org/ns/cosmos"
  xmlns:m="livcos.org/ns/map"
  xmlns:f="livcos.org/ns/xslt/function"
  xmlns="http://www.w3.org/1999/xhtml"
  exclude-result-prefixes="doc c f m">

  <xsl:import href="nav.xsl" />
  
  <xsl:template name="print_page_style">
    <link href="css/iNav.css" rel="stylesheet" type="text/css" />
  </xsl:template>
  
  <xsl:template name="print_page_script">
    <script>
      function nav(id) {
        window.location.href = "iNav.html?map=" + 
                               id.replace("#", "<xsl:text disable-output-escaping="yes">&amp;</xsl:text>item=");
      }
    </script>
  </xsl:template>
  
  <xsl:template match="m:item" mode="run">
    <html>
      <head>
        <title><xsl:value-of select="m:label"/></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <xsl:call-template name="print_page_style"/>
        <xsl:call-template name="print_page_script"/>
      </head>
      <body>
        <div class="contentPanel">
          <div class="main">
            <div id="{$mapId}" class="map" onclick="nav('{$mapId}#item_1')">
              <xsl:value-of select="$mapObj/m:head/m:title"/>
            </div>
            <div id="{@id}" class="item">
              <xsl:value-of select="substring(m:label, 0, $MAX_MAIN_LABEL_LENGTH)"/>
              <xsl:if test="string-length(m:label) &gt;= $MAX_MAIN_LABEL_LENGTH">...</xsl:if>
            </div>
            <div class="content">
              <xsl:apply-templates select="doc:*"/>
            </div>
          </div>
          <div class="rels">
            <xsl:apply-templates select="m:rels/*"/>
          </div>
        </div>
      </body>
    </html>
  </xsl:template>
  
  <xsl:template match="m:rel">
    <xsl:apply-templates select="m:item"/>
  </xsl:template>
  
  <xsl:template match="m:rel[m:item/@id = /m:item/@id]" priority="2">
  </xsl:template>
  
  <xsl:template match="m:item">
    <div id="{../@id}">
      <xsl:choose>
        <xsl:when test="@missing">
          <xsl:attribute name="class">item rel<xsl:value-of select="count(ancestor::m:item)"/> missing</xsl:attribute>
        </xsl:when>
        <xsl:otherwise>
          <xsl:attribute name="class">item rel<xsl:value-of select="count(ancestor::m:item)"/></xsl:attribute>
          <xsl:attribute name="onclick">
            <xsl:text>nav('</xsl:text>
            <xsl:value-of select="@id"/>
            <xsl:text>');</xsl:text>
          </xsl:attribute>
          <xsl:if test="doc:link">
            <a class="directClick" href="{doc:link[1]/@ref}">&#160;</a>
          </xsl:if>
        </xsl:otherwise>
      </xsl:choose>
      <xsl:value-of select="substring(m:label, 0, $MAX_LABEL_LENGTH)"/>
      <xsl:if test="string-length(m:label) &gt;= $MAX_LABEL_LENGTH">...</xsl:if>
    </div>
  </xsl:template>
  
</xsl:stylesheet>
